package com.maxuxu.chat.chat;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receiver {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);

        while (true) {
            // 准备接受包裹
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
            socket.receive(packet); // 阻塞式接受包裹

            // 断开连接
            byte[] data = packet.getData();
            String receiveData = new String(data, 0, data.length);

            System.out.println(receiveData.trim());
            if (receiveData.trim().equals("bye")){
                break;
            }

        }
        socket.close();
    }
}
